package com.htt.base_library.util

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import com.google.gson.Gson
import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.PrettyFormatStrategy
import java.math.RoundingMode
import java.text.DecimalFormat
import java.util.regex.Pattern


/**
 * 设置是否打印和打印tag
 */
fun String.logTagDebug(debug: Boolean): AndroidLogAdapter {
    val formatStrategy = PrettyFormatStrategy.newBuilder()
        .showThreadInfo(false).methodCount(1).tag(this).build()
    return object : AndroidLogAdapter(formatStrategy) {
        override fun isLoggable(priority: Int, tag: String?): Boolean {
            return debug
        }
    }
}



/**
 * base64 数据流图片  用于图片验证码
 */
fun base64Image(base64Data: String): Bitmap {
    val bytes = Base64.decode(base64Data, Base64.DEFAULT)
    return BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
}

fun Any.toJson(): String = Gson().toJson(this)

/**
 * 判断是字母
 */
fun String?.isLetterDigit(): Boolean = this?.let {
    return Pattern.compile("[a-zA-Z]+").matcher(it).matches()
} ?: false

/**
 * 判断是否是数字
 */
fun String?.isNumeric(): Boolean = this?.let {
    return Pattern.compile("[0-9]*").matcher(it).matches()
} ?: false



val Number.double_2: String
    get() {
        val format = DecimalFormat("0.00")//构造方法的字符格式这里如果小数不足4位,会以0补足.
        format.roundingMode = RoundingMode.DOWN
        return format.format(toDouble())//format 返回的是字符串
    }
val Number.double_4: String
    get() {
        val format = DecimalFormat("0.0000")//构造方法的字符格式这里如果小数不足4位,会以0补足.
        format.roundingMode = RoundingMode.DOWN
        return format.format(toDouble())//format 返回的是字符串
    }

val String?.double_2: String
    get() {
        return (this?.toDoubleOrNull() ?: 0.00).double_2
    }
val String?.double_4: String
    get() {
        return (this?.toDoubleOrNull() ?: 0.00).double_4
    }
val String?.double_0: String
    get() {
        return (this?.toDoubleOrNull() ?: 0.00).toInt().toString()
    }

